<template>
    <MyArticle title="Ubuntu20.04编译安装OpenCV4.5.0" date="2025年04月17日">
        <div class="_article-content">
            <p>OpenCv主页：<a href="https://opencv.org/releases/" target="_blank">https://opencv.org/releases/</a></p>
            <p>查看opencv版本命令：</p>
            <pre><code>root@PC-ZHANGJIALIN:/mnt/d# pkg-config --modversion opencv4
4.5.0
// 或者
root@PC-ZHANGJIALIN:/mnt/d# opencv_version
4.5.0</code></pre>
            <h3 class="title">安装依赖项</h3>
            <p>基础编译工具</p>
            <pre><code>sudo apt install -y g++ cmake make wget unzip</code></pre>
            <p>核心依赖库</p>
            <pre><code>sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev</code></pre>
            <p>可选依赖库</p>
            <pre><code>sudo apt install libpng-dev libopenexr-dev libwebp-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev</code></pre>
            <p>Python支持</p>
            <pre><code>sudo apt install python3-dev python3-numpy</code></pre>
            <h3 class="title">源码下载</h3>
            <pre><code>wget -O opencv-4.5.0.zip https://codeload.github.com/opencv/opencv/zip/refs/tags/4.5.0
wget -O opencv_contrib-4.5.0.zip https://github.com/opencv/opencv_contrib/archive/4.5.0.zip
unzip opencv-4.5.0.zip -d ./
unzip opencv_contrib-4.5.0.zip -d ./</code></pre>
            <p>将opencv_contrib-4.5.0文件夹移动到opencv-4.5.0目录下：</p>
            <pre><code>mv opencv_contrib-4.5.0 ./opencv-4.5.0
cd opencv-4.5.0</code></pre>
            <h3 class="title">编译与安装</h3>
            <p>创建构建目录</p>
            <pre><code>mkdir build && cd build
// CMake配置
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.5.0/modules -D CMAKE_INSTALL_PREFIX=/usr/local ..</code></pre>
            <p>手动下载boostdesc_bgm.i等文件，百度云：<a href="https://pan.baidu.com/s/1BeYF8kqEZLAJYQj-MvxpmA" target="_blank">https://pan.baidu.com/s/1BeYF8kqEZLAJYQj-MvxpmA</a>(提取码：e1wc)</p>
            <p>解压后将所有文件复制到opencv_contrib-4.5.0/modules/xfeatures2d/src/ 目录下</p>
            <pre><code>mv file.zip ./opencv_contrib-4.5.0/modules/xfeatures2d/src
unzip file.zip -d ./</code></pre>
            <p>拷贝modules/features2d目录至opencv_contrib-4.5.0/modules/xfeatures2d/test/下面</p>
            <pre><code>cd opencv-4.5.0
cp -r ./modules/features2d ./opencv_contrib-4.5.0/modules/xfeatures2d/test/</code></pre>
            <p>编译源码（根据CPU核心数调整-j参数</p>
            <pre><code>cd build
make -j4</code></pre>
            <p>安装到系统</p>
            <pre><code>make install</code></pre>
            <h3 class="title">环境配置</h3>
            <pre><code>// 配置pkg-config
echo 'export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH' | sudo tee /etc/profile.d/pkgconfig.sh
source /etc/profile
// 动态链接库
echo '/usr/local/lib' | sudo tee /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
// 软链接
ln -s /usr/local/include/opencv4/opencv2 /usr/local/include/opencv2
</code></pre>
            <p>配置bashrc</p>
            <pre><code>echo 'export OPENCV_INCLUDE_DIR=/usr/local/include' >> ~/.bashrc
echo 'export OPENCV_LIB_DIR=/usr/local/lib' >> ~/.bashrc
echo 'export OPENCV_BIN_DIR=/usr/local/bin' >> ~/.bashrc
source ~/.bashrc</code></pre>
        </div>
    </MyArticle>
</template>

